import numpy as np
import pyqtgraph as pg
from PyQt5.QtWidgets import QWidget


class BScanGraphPG(QWidget):
    """
    画雷达灰度图的UI类
    """
    def __init__(self, samplePoint=215):
        super().__init__()
        pg.setConfigOptions(antialias=True)
        self.resize(800, 480)
        self.data = []
        self.imv = pg.ImageView(self)
        self.radarResData = np.zeros([samplePoint, 200])
        self.samplePoint = samplePoint
        self.initFlag = 0

    def init_img(self, dataAir):
        """
        重新将UI初始化
        :return:
        """
        self.radarResData = np.zeros([self.samplePoint, 200])
        self.dataAir = dataAir.real * 10000
        self.dataAir = np.mean(self.dataAir.real.T, 1)

    def handle_data(self, dealS11):
        """
        把雷达传回输出处理后画出来， 这里分为了第一次雷达数据和之后的数据目前没什么用，但是之前或者之后可能会需要在第一次
        雷达数据到达时做一些处理。
        :param dealS11: 新的雷达数据
        :return:
        """
        dealS11 = dealS11 - self.dataAir
        if self.initFlag == 0:
            self.radarResData = np.column_stack((self.radarResData, dealS11))
            self.radarResData = np.delete(self.radarResData, 0, axis=1)
            self.imv.setImage(self.radarResData.T)
            self.initFlag = 1
        else:
            self.radarResData = np.column_stack((self.radarResData, dealS11))
            self.radarResData = np.delete(self.radarResData, 0, axis=1)
            self.imv.setImage(self.radarResData.T)
